﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Management;

namespace FastAccess
{
    static class Program
    {
        
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Config();
            CheckReg();
            Application.Run(new MainForm());
        }
        private static void Config()
        {
            Settings.WorkingDir = AppDomain.CurrentDomain.BaseDirectory;
        }
        private static void CheckReg()
        {
            string Reg = string.Empty;
            string path = Settings.WorkingDir + "Reg.Dat";
            if (File.Exists(path))
            {
                Settings.Key = Helper.GetKey();
                using (StreamReader rd = File.OpenText(path))
                {
                    Reg = rd.ReadToEnd();
                }
                if (Reg.Length > 10)
                {
                    CodeReg reg = new CodeReg(Settings.Key);
                    try
                    {
                        string Cfg = reg.Decode(Reg);
                        string[] Cfgs = Cfg.Split('|');
                        if (Cfgs.Length > 1)
                        {
                            if (Cfgs[0].Equals("true"))
                            {
                                Settings.Registed = true;

                                if (!DateTime.TryParse(Cfgs[1], out Settings.OutDate))
                                {
                                    Settings.Registed = false;
                                }
                                else
                                {
                                    if (Settings.OutDate.CompareTo(DateTime.Now) < 0)
                                    {
                                        Settings.Registed = false;
                                    }
                                }
                            }
                            else
                            {
                                Settings.Registed = false;
                            }
                        }
                    }
                    catch(Exception ex)
                    {
                        Settings.Registed = false;
                    }
                }
            }
            else
            {
                Settings.Registed = false;
            }
        }
        

    }

    class CodeReg
    {
        private byte[] _Key;
        private byte[] IV = new byte[] { 0xEE, 0xCE, 0x55, 0x78, 0x91, 0xAC, 0xCC, 0xFF };

        public CodeReg(string Key)
        {
            _Key = Encoding.UTF8.GetBytes(Key);
        }
        public string Decode(string Data)
        {
            string rs = null;
            Byte[] input = Convert.FromBase64String(Data);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                ICryptoTransform trans = des.CreateDecryptor(_Key, IV);
                Byte[] output = trans.TransformFinalBlock(input, 0, input.Length);
                rs = Encoding.UTF8.GetString(output);
            }
            catch (Exception ex)
            {
                throw new Exception(Settings.OutStr);
            }
            return rs;
        }
        public string Encode(string Data)
        {
            string rs = null;
            Byte[] input = Encoding.UTF8.GetBytes(Data);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                ICryptoTransform trans = des.CreateEncryptor(_Key, IV);
                Byte[] output = trans.TransformFinalBlock(input, 0, input.Length);
                rs = Convert.ToBase64String(output);
            }
            catch (Exception ex)
            {
                throw new Exception(Settings.OutStr);
            }
            return rs;
        }

    }
}
